package top.zhouze.common.util;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.jetbrains.annotations.NotNull;

import java.util.*;

/**
 * @author 周仄
 * @since 2023/7/28 11:57
 */
public class SetList<E> extends ArrayList<E> {
    private final Set<E> setTemp = new HashSet<>();

    @Override
    public boolean contains(@Nullable @org.jetbrains.annotations.Nullable Object o) {
        return setTemp.contains(o);
    }

    @Override
    public boolean add(E e) {
        return setTemp.add(e) && super.add(e);
    }

    @Override
    public E remove(int index) {
        E e = super.remove(index);
        setTemp.remove(e);
        return e;
    }

    @Override
    public boolean remove(@Nullable @org.jetbrains.annotations.Nullable Object o) {
        return setTemp.remove(o) && super.remove(o);
    }

    @Override
    public void clear() {
        setTemp.clear();
        super.clear();
    }

    @Override
    public boolean addAll(@NonNull @NotNull Collection<? extends E> c) {
        return setTemp.addAll(c) && super.addAll(c);
    }
}
